import { createStore } from "vuex";

export default createStore({
  // 用于储存 状态
  state: {
    username:'free'
  },
  // 基于现有状态演变新的状态
  getters: {
    newUsername(state){
      return state.username +'haha'
    }
  },
  // 改变state 数据
  mutations: {
    updateUsername(state,newValue){
      state.username = newValue
    }
  },
  // 异步操作
  actions: {
  async  updateName(ctx){
        await sleep(1000)        
        ctx.commit('updateUsername','oooo')
    }
  },
  modules: {},
})

function sleep(time:any){
  return new Promise((resolve) =>{
    setTimeout( resolve, time);
  })
}


